<?php

require_once 'PagesModel.php';
require_once 'PagesView.php';

class PagesController
{
	public function DoControll()
	{
		$msg = array();
	
		$pagesModel = new PagesModel();
		$pagesView = new PagesView();
		
		// Try to add a new webpage if the user chose to.
		if ( $pagesView->TriedToAddPage() )
		{
			if ( $pagesModel->AddPage( $pagesView->GetAddPageData() ) )
			{
				$msg['add']['msg'][] = 'Page added';
			}
			else
			{
				$msg['add']['error'][] = 'Could not add page.';
			}
		}
	
		// Try to update existing webpages if the user chose to.
		if ( $pagesView->EditSave() )
		{
			if ( $pagesModel->UpdatePage( $pagesView->GetEditPageData() ) )
			{
				$msg['edit']['msg'][] = 'Page updated successfully.';
			}
			else
			{
				$msg['edit']['error'][] = 'Could not update page.';
			}
		}
		
		// Try to delete existing webpages if the user chose to.
		if ( $pagesView->DeletePage() )
		{
			if ( $pagesModel->DeletePage( $pagesView->GetListPageID() ) )
			{
				$msg['delete']['msg'][] = 'Page successfully deleted.';
			}
			else
			{
				$msg['delete']['error'][] = 'Could not delete page.';
			}
		}
	
	
		$html = '';

		if ( $pagesView->EditPage() )
		{
			$html .= $pagesView->DoEditForm( $pagesModel->GetPage( $pagesView->GetListPageID() ) );
		}
		
		// Listing all existing web pages editable in a form.
		$html .= $pagesView->DoListPages( $pagesModel->GetPages(), $msg );
		
		// Display form for adding a new web page.
		$html .= $pagesView->DoNewPageForm( $msg );
		
		return $html;
	}
}
